﻿using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace SplashDev
{
    public partial class FrmSplash : Form
    {
        public FrmSplash()
        {
            InitializeComponent();
        }

        private void FrmSplash1_Load(object sender, EventArgs e)
        {
            InitUpdateProcess(8);
        }

        /// <summary>
        /// 进度条初始化
        /// </summary>
        /// <param name="processId"></param>
        /// <param name="leng"></param>
        private void InitUpdateProcess(int leng)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<int>(InitUpdateProcess), new object[] { leng });
            }
            else
            {
                if (leng > 0)
                {
                    labMsg.Text = "电商舆情监控系统正在加载中，请稍候......";

                    //progressBar1.Value = 0;
                    progressMsg.Step = 1;
                    progressMsg.Minimum = 0;
                    progressMsg.Maximum = leng;
                }
            }
        }

        private void ShowUpdateProcessing(string msg, int iStep)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string, int>(ShowUpdateProcessing), new object[] { msg, iStep });
            }
            else
            {
                progressMsg.Show();
                labMsg.Text = msg;
                //progressBar1.ca = iStep.ToString();
                //progressBar1.Value = iStep;
                progressMsg.PerformStep();
                //LogHelper.WriteInfoLogEx(msg);
            }
        }

        private void InitSys()
        {
            ShowUpdateProcessing("####################################################################", 0);
            ShowUpdateProcessing("电商舆情监控平台系统初始化开始", 1);
            Thread.Sleep(1000);

            ShowUpdateProcessing("获取当前拷贝宿主机系统信息......", 1);
            Application.DoEvents();
            Thread.Sleep(1000);

            ShowUpdateProcessing("当前拷贝有效性校验......", 1);
            Application.DoEvents();
            Thread.Sleep(1000);

            ShowUpdateProcessing("加载系统运行配置文件......", 1);
            //GetLoadJson();
            Application.DoEvents();
            Thread.Sleep(1000);

            ShowUpdateProcessing("加载商品采集配置文件......", 1);
            //GetGatherJson();
            Application.DoEvents();
            Thread.Sleep(1000);

            ShowUpdateProcessing("开始数据库访问校验......", 1);
            //CheckDatabase();
            Application.DoEvents();
            Thread.Sleep(1000);

            ShowUpdateProcessing("缓存商品数据......", 1);
            //GetLocalWares();
            Thread.Sleep(1000);
            Application.DoEvents();

            ShowUpdateProcessing("系统初始化完成，开始加载主操作界面", 1);

            Thread.Sleep(1000);
            CloseSplash();
            
            //nextClipHwnd = SetClipboardViewer(this.Handle);
        }

        private void FrmSplash1_Shown(object sender, EventArgs e)
        {
            labMsg.BackColor = Color.Transparent;
            labMsg.Parent = pictureBox1;
            labMsg.ForeColor = Color.White;

            Thread thread = new Thread(delegate () { InitSys(); }) { Name = "splashThread", IsBackground = true };
            thread.Start();
        }

        /// <summary>
        /// Close the splash (Loading...) screen
        /// </summary>
        public void CloseSplash()
        {
            // need to call on the thread that launched this splash
            if (this.InvokeRequired)
                this.Invoke(new MethodInvoker(CloseSplash));
            else
                this.Close();
            //Application.ExitThread();
        }
    }
}
